/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bbmangadownloader.gui.control;

import bbmangadownloader.ult.OSSupport;
import bbmangadownloader.ult.OSSupport.OS;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;

/**
 *
 * @author Bach
 */
public class JFolderChooser extends javax.swing.JPanel {

    private File selectedFile = null;
    private FolderChooserControl fchBrowse;

    public JFolderChooser() {
        initComponents();
        getFileChooser().setMultipleMode(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtFilePath = new javax.swing.JTextField();
        btnBrowse = new javax.swing.JButton();

        btnBrowse.setText("Browse");
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(txtFilePath, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBrowse))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txtFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnBrowse))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        getFileChooser().show();
        File f = fchBrowse.getSelectedFile();
        if (f != null) {
            selectedFile = f;
            txtFilePath.setText(f.getAbsolutePath());
        }
    }//GEN-LAST:event_btnBrowseActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JTextField txtFilePath;
    // End of variables declaration//GEN-END:variables

    public File getSelectedFolder() {
        File f = new File(txtFilePath.getText());
        if (f.isDirectory() && f != selectedFile) {
            selectedFile = f;
        }
        return selectedFile;
    }

    public void setSelectedFile(File file) throws ExceptionInInitializerError {
        file = new File(file.getAbsolutePath());
//        if (file.isDirectory()) {
        selectedFile = file;
        fchBrowse.setSelectedFile(file);
        txtFilePath.setText(file.getAbsolutePath());
//        } else {
//            throw new ExceptionInInitializerError("inputFile must be directory");
//        }
    }

    public JButton getButtonBrowse() {
        return btnBrowse;
    }

    private FolderChooserControl getFileChooser() {
        if (fchBrowse == null) {
            fchBrowse = new FolderChooserControl();
        }
        return fchBrowse;
    }

    public JTextField getTextField() {
        return txtFilePath;
    }

    private class FolderChooserControl {

        private FileDialog fchFileDialog;
        // Use in Mac OS
        private JFileChooser fchFileChooser;
        // Use in Windows

        private FolderChooserControl() {
            if (OSSupport.getOS() == OS.MAC_OS) {
                fchFileDialog = new FileDialog((Frame) null, "Choose directory");
            } else {
                fchFileChooser = new JFileChooser();
                fchFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            }
        }

        private void setMultipleMode(boolean flag) {
            if (fchFileDialog == null) {
                fchFileChooser.setMultiSelectionEnabled(flag);
            } else {
                fchFileDialog.setMultipleMode(flag);
            }
        }

        private File getSelectedFile() {
            if (fchFileDialog == null) {
                return fchFileChooser.getSelectedFile();
            } else {
                return new File(fchFileDialog.getDirectory(), fchFileDialog.getFile());
            }
        }

        private void setSelectedFile(File selectedFile) {
            if (fchFileDialog == null) {
                fchFileChooser.setSelectedFile(selectedFile);
            } else {
                fchFileDialog.setDirectory(selectedFile.getAbsolutePath());
            }
        }

        private void show() {
            if (fchFileDialog == null) {
                fchFileChooser.showDialog(JFolderChooser.this, "Choose");
            } else {
                if (OSSupport.getOS() == OS.MAC_OS) {
                    System.setProperty("apple.awt.fileDialogForDirectories", "true");
                    fchFileDialog.setVisible(true);
                    System.setProperty("apple.awt.fileDialogForDirectories", "false");
                } else {
                    fchFileDialog.setVisible(true);
                }
            }
        }
    }
}
